package com.bjpn.settings.controller;


import com.bjpn.settings.bean.User;
import com.bjpn.settings.service.UserService;
import com.bjpn.util.MessageUtil;
import com.bjpn.util.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzy
 * @since 2022-09-06
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/updatePwd.action")
    @ResponseBody
    public ReturnObject updatePwd(String newPwd, ReturnObject robj, HttpSession session){
        User user = (User) session.getAttribute("user");
        user.setLoginPwd(newPwd);
        int num = userService.updatePwdById(user);
        if (num > 0) {
            // 更改session里面的user信息
            session.setAttribute("user", user);
            robj.setMessageCode(MessageUtil.UPDATE_SUCCESS_CODE);
            robj.setMessageStr(MessageUtil.UPDATE_SUCCESS_MESSAGE);
        }else {
            // 修改失败
            robj.setMessageCode(MessageUtil.UPDATE_ERROR_CODE);
            robj.setMessageStr(MessageUtil.UPDATE_ERROR_MESSAGE);
        }
        return robj;
    }

    // 加载所有员工到前端 满足条件的员工
    @RequestMapping("/getAllUser.action")
    @ResponseBody
    public List<User> getAllUser(){
        return userService.getAllUserAvailable();
    }

    // 按id查找user
    @RequestMapping("/getUsersById.action")
    @ResponseBody
    public User getUsersById(Integer activityOwner){
        return userService.getById(activityOwner);
    }
}

